﻿using System.Collections;
using System.Collections.Generic;
using ILRuntime.Runtime.Enviorment;
using UnityEngine;

public class ILRApp : MonoBehaviour
{
    AppDomain appdomain;

    // Start is called before the first frame update
    async void Start()
    {
        appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
        var ret = await LoadUtils.LoadAsync("HotFix_Project");
        appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
        var lib = await LoadUtils.LoadAsync("Lib");
        
        //如果在dll里调用了另一个dll的方法，要两个dll都加载好
        appdomain.LoadAssembly(ret.DllStream, ret.PDBStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
        appdomain.LoadAssembly(lib.DllStream, lib.PDBStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
        InitializeILRuntime();
        OnHotFixLoaded();
    }

    void InitializeILRuntime()
    {
        //这里做一些ILRuntime的注册，HelloWorld示例暂时没有需要注册的
    }

    void OnHotFixLoaded()
    {
        //HelloWorld，第一次方法调用
        appdomain.Invoke("HotFix_Project.MainClass", "StaticFunTest", null, null);

        object obj = appdomain.Instantiate("HotFix_Project.MainClass", null);
        var ret = appdomain.Invoke("HotFix_Project.MainClass", "Main", obj, null);
        Debug.Log(ret);
    }

    private void OnDestroy()
    {
        LoadUtils.ClearDLLStream();
    }
}